import { queryVehicleDetail } from '../../services/vehicle'
import { queryOrderInfoDetail, queryOrderProjectList, inspectorSaveOrder, completeOrder } from '../../services/order'
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:"",
    vehicleId:"",
    orderInfo:{},
    vehicleAllInfo:{},
    vehicleInfo:{
      title:'补充车辆信息',
      label1: '车牌:',
      value1: "",
      label2: "车型:",
      value2: "",
      url:'/pages/maintain-vehicle/maintain-vehicle'
    },
    orderProjectList:[],
    tempStorageOrder:[],
    showDialogConfirm:false,
    showDialogInput:true
  },

  onLoad(options){
    if(!options){
      return
    }
    console.log("options>>", options)
    const id = options.id
    const vehicleId = options.vehicleId
    this.setData({
      orderId: id
    })
    
    this.getOrderProjectList(id)
    if(vehicleId){
      this.setData({
        vehicleId
      })
      this.getVehicleInfo(vehicleId)
    }
    this.getOrderDetailInfo(id)
  },

  onShow(){
    if(this.data.vehicleId){
      this.getVehicleInfo(this.data.vehicleId)
    }
    // if(this.data.orderId){
    //   this.getOrderDetailInfo(this.data.orderId)
    // }
  },

  async getOrderDetailInfo(orderId){
    let orderRes = await queryOrderInfoDetail(orderId)
    if(orderRes.code === 200){
      this.setData({
        orderInfo: orderRes.data
      })
      if(!this.data.vehicleId){
        let vehicleId = orderRes.data.vehicleId
        this.setData({
          vehicleId
        })
        this.getVehicleInfo(vehicleId)
      }
    }
  },

  async getOrderProjectList(orderId){
    let orderRes = await queryOrderProjectList({ orderId })
    if(orderRes.code === 200){
      this.setData({
        orderProjectList: orderRes.rows.sort(function(a, b){return a.type - b.type}),
        tempStorageOrder: orderRes.rows.sort(function(a, b){return a.type - b.type}),
      })
      wx.setStorageSync('tempStorageOrder', orderRes.rows.sort(function(a, b){return a.type - b.type}))
    }
  },

  async getVehicleInfo(vehicleId){
    let vehicleRes = await queryVehicleDetail(vehicleId)
    if(vehicleRes.code === 200){
      console.log("vehicleRes???", vehicleRes)
      if(vehicleRes.data){
        this.setData({
          vehicleAllInfo: vehicleRes.data || {},
          ['vehicleInfo.value1']: vehicleRes.data.plateNo || "",
          ['vehicleInfo.value2']:vehicleRes.data.vehicleType || "",
          ['vehicleInfo.url']:`/pages/maintain-vehicle/maintain-vehicle?currentId=${vehicleRes.data.id}`,
        })
      }
    }
  },

  handleIconClick(event){
    console.log("icon>>>event>>>>", event)
  },

  handleInputClick(event){
    console.log("input>>>event>>>", event)
  },

  navToInspect(event){
    let orderId = this.data.orderId
    let vehicleId = this.data.vehicleId
    let instId = event.currentTarget.dataset.instid
    let projectId = event.currentTarget.dataset.projectid
    let currentItem = event.currentTarget.dataset.currentitem
    let url = ""
    if(currentItem.type == 1){
      url = `/pages/inspect-appearance/inspect-appearance?orderId=${orderId}&projectId=${projectId}&instId=${instId}`
    } else {
      url = `/pages/inspect-warning/inspect-warning?type=${currentItem.type}&orderId=${orderId}&vehicleId=${vehicleId}&instId=${instId}`
    }
    const _this = this
    wx.navigateTo({
      url: url,
      events:{
        instItemChange(data){
          console.log("跳转页面接收到的数据>>>",data)
          _this.getOrderProjectList(data.orderId)
          // const tempStorageOrder = _this.data.tempStorageOrder
          // const newIndex = tempStorageOrder.findIndex(item => item.id === data.id)
          // if(newIndex == -1){
          //   tempStorageOrder.push(data)
          // }else{
          //   tempStorageOrder[newIndex] = data
          // }
          // _this.setData({
          //   tempStorageOrder
          // })
          // wx.setStorageSync('tempStorageOrder', tempStorageOrder)
        }
      }
    })
  },

  async handleSaveClick(){
    let saveRes = await inspectorSaveOrder(this.data.tempStorageOrder)
    if(saveRes.code === 200){
      Toast.success('保存成功!');
      this.getOrderDetailInfo(this.data.orderId)
    }
  },

  handleCheckIsOk(){
    let orderProjectList = this.data.orderProjectList
    let res = true
    for(let item of orderProjectList){
      if(item.status != 3){
        res = false
      }
    }
    return res
  },

  async submitInfo(){
    let checkIsOk = this.handleCheckIsOk()
    console.log("checkIsOk>>>", checkIsOk)
    if(!checkIsOk){
      this.setData({
        showDialogConfirm:true
      })
    }else{
      let dataInfo = {
        id: this.data.orderId,
      }
      const submitRes = await completeOrder(dataInfo)
      console.log("submitRes>>", submitRes)
      if(submitRes.code === 200){
        Toast.success('提交成功!');
        setTimeout(function(){
          wx.reLaunch({
            url:'/pages/home-page/home-page?tabIndex=all'
          })
        },2000)
      }
    }
  },

  async handleConfirm(event){
    console.log("event>>>",event)
    let remark = event.detail.value
    let dataInfo = {
      id: this.data.orderId,
      remark
    }
    const submitRes = await completeOrder(dataInfo)
    console.log("submitRes>>", submitRes)
    if(submitRes.code === 200){
      Toast.success('提交成功!');
      setTimeout(function(){
        wx.reLaunch({
          url:'/pages/home-page/home-page?tabIndex=all'
        })
      },2000)
    }
  }
})